﻿using System.Collections.Generic;

namespace AzukiMap.MapSource
{
    public class YahooMapSource : MercatorQuadTileSource
    {
        YahooMapTileType _tileType;
        Dictionary<YahooMapTileType, string> _tileVersions;
        Dictionary<YahooMapTileType, string> _additionalParams;
        
        public YahooMapTileType TileType
        {
            get { return _tileType; }
        }

        public Dictionary<YahooMapTileType, string> TileVersions
        {
            get { return _tileVersions; }
            set { _tileVersions = value; }
        }

        public Dictionary<YahooMapTileType, string> AdditionalParams
        {
            set { _additionalParams = value; }
            get { return _additionalParams; }
        }

        public YahooMapSource(YahooMapTileType layerType)
            : base("YMap Source")
        {
            init(layerType);
        }

        public YahooMapSource(YahooMapTileType layerType, string name)
            : base(name)
        {
            init(layerType);
        }

        void init(YahooMapTileType layerType)
        {
            _tileType = layerType;
            _dpi = 96;
            _tileWidth = 256;
            _tileHeight = 256;

            _tileVersions = new Dictionary<YahooMapTileType, string>();
            _additionalParams = new Dictionary<YahooMapTileType, string>();

            SetBaseUris();
            SetScales();
            SetTileVersions();
            SetAddtionaParams();
        }

        //http://maps2.yimg.com/hx/tl?v=4.2&.intl=en&x=58156&y=7007&z=17&r=1
        //http://maps3.yimg.com/ae/ximg?v=1.9&t=a&s=256&.intl=en&x=1817&y=217&z=12&r=1
        //http://maps1.yimg.com/hx/tl?v=4.2&t=h&.intl=en&x=1816&y=217&z=12&r=1

        void SetBaseUris()
        {
            switch (_tileType)
            {
                case YahooMapTileType.MAP:
                    _baseUris.Add("http://maps1.yimg.com/hx/tl?");
                    _baseUris.Add("http://maps2.yimg.com/hx/tl?");
                    break;
                case YahooMapTileType.SATELITE:
                    _baseUris.Add("http://maps3.yimg.com/ae/ximg?");
                    break;
                case YahooMapTileType.HYBRID_LABEL:
                    _baseUris.Add("http://maps1.yimg.com/hx/tl?");
                    break;
                default:
                    _tileType = YahooMapTileType.MAP;
                    _baseUris.Add("http://maps1.yimg.com/hx/tl?");
                    _baseUris.Add("http://maps2.yimg.com/hx/tl?");
                    break;
            }
        }

        void SetTileVersions()
        {
            _tileVersions.Add(YahooMapTileType.MAP, "v=4.2");
            _tileVersions.Add(YahooMapTileType.SATELITE, "v=1.0");
            _tileVersions.Add(YahooMapTileType.HYBRID_LABEL, "v=4.2");

        }

        void SetAddtionaParams()
        {
            _additionalParams.Add(YahooMapTileType.MAP, ".intl=en");
            _additionalParams.Add(YahooMapTileType.SATELITE, "t=a&s=256&.intl=en");
            _additionalParams.Add(YahooMapTileType.HYBRID_LABEL, "t=h&.intl=en");
        }

        void SetScales()
        {
            SetZoomLevels();
            foreach (KeyValuePair<double, int> level in _scaleLevels)
            {
                _scales.Add(level.Key);
            }
            _scales.Sort();
        }

        void SetZoomLevels()
        {
            // YM scale number starts from 2!
            _scaleLevels.Add(295829355.5,2);
            _scaleLevels.Add(147914677.7,3);
            _scaleLevels.Add(73957338.86,4);
            _scaleLevels.Add(36978669.43,5);
            _scaleLevels.Add(18489334.72,6);
            _scaleLevels.Add(9244667.36,7);
            _scaleLevels.Add(4622333.68,8);
            _scaleLevels.Add(2311166.84,9);
            _scaleLevels.Add(1155583.42,10);
            _scaleLevels.Add(577791.71,11);
            _scaleLevels.Add(288895.85,12);
            _scaleLevels.Add(144447.93,13);
            _scaleLevels.Add(72223.96,14);
            _scaleLevels.Add(36111.98,15);
            _scaleLevels.Add(18055.99,16);
            _scaleLevels.Add(9028,17);
        }

    }

    public enum YahooMapTileType
    {
        MAP,
        HYBRID_LABEL,
        SATELITE,
    }
}
